<?php

/**
 * Created by PhpStorm.
 * User: luzhaoyang
 * Date: 2017/6/23
 * Time: 12:32
 */
class Coupon extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function getCoupons()
    {
        $sls = $this->input->get('sls');
        if (!isset($sls) || empty($sls)) $this->error();

        $this->load->model('kdtsalesman_model');
        $res = $this->kdtsalesman_model->getCoupons($sls);
        $status = array('status' => 1, 'data' => array());
        if ($res === false) $status['status'] = 2;
        else $status['data'] = $res;
        $this->success($status);
    }

    public function take()
    {
        $alias=$this->input->get('alias');
        if (!isset($alias)||empty($alias)) $this->error('system error');
        $alias=explode('$',$alias);
        $len=count($alias);
        if ($len<2) $this->error('system error');
        $couponid = $alias[1];
        $sls = $alias[0];

        //跳转绑定客户关系，废除，有赞更新，不能跳转非有赞域名的链接
//        if (!isset($alias[2])||$alias[2]!=='redirect') {
//            redirect('https://h5.youzan.com/v2/trade/directsellerJump/jump?kdt_id=18168297&sl='.$sls.'&redirect_uri='.
//            'http://api.nldou.com/salesman/coupon/take?alias='.$sls.'$'.$couponid.'$redirect');
//            exit();
//        }

        if(!isset($_SESSION)) session_start();
        if (!isset($_SESSION['wx_openid'])) redirect('http://api.nldou.com/ucenter/weixinlogin?pid=5626&page=&param=alias.'.$sls.'$'.$couponid);

        $ret = array('status'=>1,'msg'=>'','coupon'=>array(),'value'=>0,'url'=>'','shareurl'=>'');
        $ret['shareurl']='http://api.nldou.com/ucenter/weixinlogin?pid=5626&page=&param=alias.'.$sls.'$'.$couponid;
        $redirecturi=urlencode('https://h5.youzan.com/v2/showcase/homepage?kdt_id=18168297&sls='.$sls);
        //领券跳转绑定客户关系
        $ret['url']='https://h5.youzan.com/v2/trade/directsellerJump/jump?'.
            'kdt_id=18168297&sl='.$sls.'&redirect_uri='.$redirecturi;
        $this->load->model('kdtsalesman_model');
        $data=$this->kdtsalesman_model->checkExsit('kdt_coupons','description,value',array('couponid'=>$couponid));
        $ret['value']=(float)$data['value']/100;
        if ($data===false){
            $ret['status']=-1;
            $ret['msg']='该优惠券使用期限已过，不能领取';
        }else{
            $this->load->model('kdt_api_model');
            $res=$this->kdt_api_model->takeCoupon($couponid,$_SESSION['wx_openid']);
            if (isset($res['error_response'])){
                $ret['status']=-1;
                $ret['msg']=$res['error_response']['msg'];
            }else{
                $coupontype=strtolower($res['response']['coupon_type']);
                $ret['coupon']=$res['response'][$coupontype];
                $ret['coupon']['description']=$data['description'];
            }
        }
        $this->load->view('salesman/coupon',$ret);
    }

    public function taketest()
    {
        $alias=$this->input->get('alias');
        if (!isset($alias)||empty($alias)) $this->error('system error');
        $alias=explode('$',$alias);
        $len=count($alias);
        if ($len<2) $this->error('system error');
        $couponid = $alias[1];
        $sls = $alias[0];
//        echo $alias.PHP_EOL;
//        echo $couponid.PHP_EOL;
//        echo $sls.PHP_EOL;
//        exit();

        //跳转绑定客户关系
//        if (!isset($alias[2])||$alias[2]!=='redirect') {
//            $redurl = 'https://h5.youzan.com/v2/trade/directsellerJump/jump?kdt_id=18168297&sl='.$sls.'&redirect_uri=';
//            $redurl .= urlencode('http://api.nldou.com/salesman/coupon/taketest?alias='.$sls.'$'.$couponid.'$redirect');
//            var_dump($redurl);exit();
//            redirect($redurl);
//            exit();
//        }

        if(!isset($_SESSION)) session_start();
        if (!isset($_SESSION['wx_openid'])) redirect('http://api.nldou.com/ucenter/weixinlogin?pid=5626&page=&param=alias.'.$sls.'$'.$couponid);

        $ret = array('status'=>1,'msg'=>'','coupon'=>array(),'value'=>0,'url'=>'','shareurl'=>'');
        $ret['shareurl']='http://api.nldou.com/ucenter/weixinlogin?pid=5626&page=&param=alias.'.$sls.'$'.$couponid;
        $redirecturi=urlencode('https://h5.youzan.com/v2/showcase/homepage?kdt_id=18168297&sls='.$sls);
        //领券跳转绑定客户关系
        $ret['url']='https://h5.youzan.com/v2/trade/directsellerJump/jump?'.
            'kdt_id=18168297&sl='.$sls.'&redirect_uri='.$redirecturi;
        $this->load->model('kdtsalesman_model');
        $data=$this->kdtsalesman_model->checkExsit('kdt_coupons','description,value',array('couponid'=>$couponid));
//        var_dump($data);
//        echo PHP_EOL;
//        var_dump($ret);
//        exit();
        if ($data===false){
            $ret['status']=-1;
            $ret['msg']='该优惠券使用期限已过，不能领取';
        }else{
            $ret['value']=(float)$data['value']/100;
            $this->load->model('kdt_api_model');
            $res=$this->kdt_api_model->takeCoupon($couponid,$_SESSION['wx_openid']);
            if (isset($res['error_response'])){
                $ret['status']=-1;
                $ret['msg']=$res['error_response']['msg'];
            }else{
                $coupontype=strtolower($res['response']['coupon_type']);
                $ret['coupon']=$res['response'][$coupontype];
                $ret['coupon']['description']=$data['description'];
            }
        }
        $this->load->view('salesman/coupon',$ret);
    }

    public function getSalesmanItems()
    {
        $this->load->model('kdt_api_model');
        $this->kdt_api_model->getSalesmanItems();
    }

    public function getSalesmanTrades()
    {
        $this->load->model('kdt_api_model');
        $this->kdt_api_model->getSalesmanTrades();
    }
}